<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 
        任何实现Iterator接口的函数都可以作为迭代器使用
     -->
    <script>
        class Counter {
            constructor(limit) {
                this.count = 1
                this.limit = limit
            }

            [Symbol.iterator]() {
                return {
                    next() {
                        if (this.count <= this.limit) {
                            return {
                                done: false, value: this.count++
                            }
                        } else {
                            return {
                                done: true,
                                value: undefined
                            }
                        }
                    }
                }
            }
        }

        const counter = new Counter(3)

        console.log(counter)

        for (let i of counter) {
            console.log(i) // 1 2 3
        }
    </script>
</body>

</html>